// +----------------------------------------------------------------------
// | bossHongshi [鸿时互娱管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.hongshihuyu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wgh
// +----------------------------------------------------------------------
const SMSClient = require('@alicloud/sms-sdk');

module.exports = class extends think.cmswing.center {
  // 我的邀请码
  async indexAction() {
	//const uid = this.get('uid') || this.user.uid;
  }

    // 手机验证码
  async verifcodeAction() {
    const data = this.post();
    console.log("data----:"+JSON.stringify(data));
    if(think.isEmpty(data.phone)){
	   return this.fail('手机号不能为空！');
    }else {
       if(data.type==1){
          let res = await this.model('member').where({mobile:data.phone}).find();
            if (!think.isEmpty(res)) {
            return this.fail('手机号码已存在，请重新填写！');
         }
       }
    }
   
    const accessKeyId = 'LTAIqO5rTv5t3O3w';
    const secretAccessKey = 'mExu7yWgbyAeD4Yxr4iJHfPiccFzCC';
    
    //初始化sms_client
    let smsClient = new SMSClient({accessKeyId, secretAccessKey});
    //发送短信
    const timestr = new Date().getTime().toString();
    const code = timestr.substr(7);
    console.log("短信验证码--:"+code);
    var phone = data.phone.toString();
    var templateCode = (data.type==1?'SMS_147225099':'SMS_147225098');
    console.log("templateCode--:"+templateCode);
    let ret = await smsClient.sendSMS({
      PhoneNumbers:phone,//必填:待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式；发送国际/港澳台消息时，接收号码格式为：国际区号+号码，如“85200000000”
      SignName: '鸿时互娱',//必填:短信签名-可在短信控制台中找到
      TemplateCode: templateCode,//必填:短信模板-可在短信控制台中找到，发送国际/港澳台消息时，请使用国际/港澳台短信模版
      TemplateParam: '{"code":"'+code+'"}'//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时。
    }).catch(() => false);

    //let ret ={"Message":"OK","RequestId":"8C497A6A-E234-4477-9966-B31CFC78F001","BizId":"102700243039236874^0","Code":"OK"}
     console.log('ret ---------:'+JSON.stringify(ret));

    if(ret.Code== 'OK'){
      console.log("发送成功!--:"+JSON.stringify(ret));
      var smsverifcode={};
        smsverifcode.mobile=phone;
        smsverifcode.validate_code = code;
        smsverifcode.type = data.type;
        var dd = new Date();
        // var day2 = dd.valueOf()+(Number(this.config('setup.SAFEBOX_DELAY')) * 60000);
        var expire_time = dd.valueOf()+(Number(10) * 60000);
        smsverifcode.create_time = dd.valueOf();
        smsverifcode.expire_time =expire_time;
        await this.model('sms_verifcode').add(smsverifcode);
        return this.success("发送成功!");
    }else{
      console.log("发送失败!--:"+JSON.stringify(ret));
      return this.fail("发送失败!");
    }
    
  }

};